package com.novellotus.iov.carvice.activity;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.novellotus.iov.carvice.R;
import com.novellotus.iov.carvice.adapter.DiagnosisFragmentAdapter;
import com.novellotus.iov.carvice.bean.RxDiagnosisAlarm;
import com.novellotus.iov.carvice.fragment.ChartFragment;
import com.novellotus.iov.carvice.fragment.CurrentChargeGraphFragment;
import com.novellotus.iov.carvice.fragment.CurrentGraphFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;


public class DiagnosisDataActivity extends BaseActivity {

    @BindView(R.id.tv_tittle)
    TextView tv_tittle;
    @BindView(R.id.tabLayout)
    TabLayout tableLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;

    DiagnosisFragmentAdapter diagnosisFragmentAdapter;
    List<Fragment> fragmentList;
    private RxDiagnosisAlarm rxDiagnosisAlarm;
    private String e = "", r = "", s = "", vin = "", plate = "", time = "";

    @Override
    protected void initContentView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_diagnosis_data);
    }

    @Override
    public void init() {
        if (getIntent().getExtras() != null) {
            vin = getIntent().getExtras().getString("vin");
            plate = getIntent().getExtras().getString("plate");
            e = getIntent().getExtras().getString("e");
            r = getIntent().getExtras().getString("r");
            s = getIntent().getExtras().getString("s");
            time = getIntent().getExtras().getString("time");
            rxDiagnosisAlarm = getIntent().getExtras().getParcelable("alarm");
        }

        tv_tittle.setText("车辆诊断数据");
        fragmentList = new ArrayList<>();
        if (rxDiagnosisAlarm.getCharging() == 0) {
            fragmentList.add(CurrentGraphFragment.newInstance(rxDiagnosisAlarm));
        } else {
            fragmentList.add(CurrentChargeGraphFragment.newInstance(rxDiagnosisAlarm));
        }

        fragmentList.add(ChartFragment.newInstance(vin, e, r, time));
        diagnosisFragmentAdapter = new DiagnosisFragmentAdapter(getSupportFragmentManager(), fragmentList);
        viewPager.setAdapter(diagnosisFragmentAdapter);
        tableLayout.setupWithViewPager(viewPager);
        initTabView();
    }

    void initTabView() {

        TabLayout.Tab tabNotDo = tableLayout.getTabAt(0);
        View viewNot = View.inflate(this, R.layout.layout_diagnosis_tab, null);
        TextView tvNotDoing = viewNot.findViewById(R.id.tv_tab);
        tvNotDoing.setText("车辆电流图");
        if (tabNotDo != null) {
            tabNotDo.setCustomView(viewNot);
        }

        TabLayout.Tab tabDoing = tableLayout.getTabAt(1);
        View view = View.inflate(this, R.layout.layout_diagnosis_tab, null);
        TextView tvDoing = view.findViewById(R.id.tv_tab);
        tvDoing.setText("车辆数据");
        if (tabDoing != null) {
            tabDoing.setCustomView(view);
        }
    }

    @OnClick(R.id.img_back)
    void back() {
        finish();
    }


}
